<?php

namespace app\admin\controller\recommend;

use ba\Tree;
use Throwable;
use think\facade\Db;
use app\admin\model\AdminGroup;
use app\common\controller\Backend;




class Relationship extends Backend
{
    /**
     * 修改、删除分组时对操作管理员进行鉴权
     * 本管理功能部分场景对数据权限有要求，修改此值请额外确定以下的 absoluteAuth 实现的功能
     * allAuthAndOthers=管理员拥有该分组所有权限并拥有额外权限时允许
     */
    protected string $authMethod = 'allAuthAndOthers';

    /**
     * 数据模型
     * @var object
     * @phpstan-var AdminGroup
     */
    protected object $model;

    /**
     * @var Tree
     */
    protected Tree $tree;

    /**
     * 远程select初始化传值
     * @var array
     */
    protected array $initValue;


    /**
     * 是否组装Tree
     * @var bool
     */
    protected bool $assembleTree;


    protected string|array $quickSearchField = ['nickname'];

    protected array $withJoinTable = ['level', 'shopUser', 'partner'];

    protected bool $modelSceneValidate = true;
    /**
     * 登录管理员的角色组
     * @var array
     */
    protected array $adminGroups = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\shop\user\User;
        $this->tree  = Tree::instance();

        $isTree          = $this->request->param('isTree', true);
        $this->initValue = $this->request->get("initValue/a", []);
        $this->initValue = array_filter($this->initValue);

        // 有初始化值时不组装树状（初始化出来的值更好看）
        $this->assembleTree = $isTree && !$this->initValue;

        $this->adminGroups = Db::name('admin_group_access')->where('uid', $this->auth->id)->column('group_id');
    }

    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        $this->success('', [
            'list'   => $this->getGroups(),
            'group'  => [],
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 获取分组
     * @param array $where
     * @return array
     * @throws Throwable
     */
    private function getGroups(array $where = []): array
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $limit = 99999999999999;
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $res->visible(['level' => ['name'], 'partner' => ['name']])->each(function ($item) {
            $item->mobile = substr_replace($item->mobile, '****', 3, -4);
            if($item->grade_id){
                $item->grade_name = Db::name('shop_user_grade')->where('id',$item->grade_id)->value('name');
            }
            if($item->pid){
                $item->pidname = $this->model->where('id',$item->pid)->value('nickname');
            }
        });
        // 获取第一个权限的名称供列表显示-e

        // 如果要求树状，此处先组装好 children
        return $this->assembleTree ? $this->tree->assembleChild($res->items()) : $res->items();
    }



}